﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.WaterManagement
{
    public class FlowOrganizationController : Controller
    {
        //
        // GET: /FlowOrganization/
        ServiceReference1.Service1Client wsdcser = new ServiceReference1.Service1Client();
        ZHHQSERVICE.Service1Client service = new ZHHQSERVICE.Service1Client();
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取商户
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer(JqGridParam jqgridparam)
        {
            try
            {
                Page<View_Order_MerSet> page = new Page<View_Order_MerSet>();//声明实体
                List<Model.View_Order_MerSet> List = new List<Model.View_Order_MerSet>();//声明实体
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];//获取oper

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = wsdcser.GetOrder_MerByOperId(user.OperID, "", 1);//调用服务接口
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_MerSet>.JsonList(jieguo);//解析返回的结果
                    jqgridparam.records = List.Count;
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(List, ref jqgridparam)
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = List
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
        }
        /// <summary>
        /// 获取所有企业流水
        /// </summary>
        /// <param name="MerID"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult getAllFlow(string MerID,JqGridParam jqgridparam)
        {
          try
           {
               Page<View_Order_FlowOrganization> page = new Page<View_Order_FlowOrganization>();//声明实体
               List<Model.View_Order_FlowOrganization> List = new List<Model.View_Order_FlowOrganization>();//声明实体
            string result = wsdcser.GetOrgFlowListFromMer(jqgridparam.page, jqgridparam.rows, MerID, "", "",2, "", 1);
            JObject jb = JObject.Parse(result);//对结果序列化
            string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> resul = jb["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_FlowOrganization>.JsonList(resul);//解析返回的结果
                    //jqgridparam.records = ParseClass<Model.Order_FlowOrganization_T>.JsonList(resul).Count;
                    foreach (Model.View_Order_FlowOrganization item in List)
                    {
                        item.OrgFlowTime = DateTimeHelper.ToDate2(item.OrgFlowTime);
                        if (item.OrgFlowTime.Length == 14)
                        {
                            item.OrgFlowTime = DateTimeHelper.ToDate2(item.OrgFlowTime);
                        }
                    }
                    var data = new
                    {
                        total = jb["total"].ToString(),
                        rows = List
                    };
                    return Json(data,JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = new
                    {
                        total =0,
                        rows = List
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
        }
        /// <summary>
        /// 查询企业账号流水
        /// Flat:0未平账/1平账/2所有
        /// </summary>
        /// <param name="Mer"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult searchForm(string Mer, string StartTime, string EndTime,string PZ, JqGridParam jqgridparam)
        {
            try
            {
                StartTime=StartTime.Replace("-","")+"000000";
                EndTime = EndTime.Replace("-", "") + "235959";
                int Flat = 0;
                Page<View_Order_FlowOrganization> page = new Page<View_Order_FlowOrganization>();//声明实体
                List<Model.View_Order_FlowOrganization> List = new List<Model.View_Order_FlowOrganization>();//声明实体
                if (PZ != null)
                {
                    Flat = int.Parse(PZ);
                }
                else
                {
                    Flat = 2;
                }
                string result = wsdcser.GetOrgFlowListFromMer(jqgridparam.page, jqgridparam.rows, Mer, StartTime, EndTime, Flat, "", 1);
                JObject jb = JObject.Parse(result);//对结果序列化
                string bs = jb["BS"].ToString();
                if (bs == "1")
                {
                    IList<JToken> resul = jb["rows"].Children().ToList();//解析返回的结果
                    List = ParseClass<Model.View_Order_FlowOrganization>.JsonList(resul);//解析返回的结果
                    foreach (Model.View_Order_FlowOrganization item in List)
                    {
                        item.OrgFlowTime = DateTimeHelper.ToDate2(item.OrgFlowTime);
                        if (item.OrgFlowTime.Length == 14)
                        {
                            item.OrgFlowTime = DateTimeHelper.ToDate2(item.OrgFlowTime);
                        }
                    }
                    var data = new
                    {
                        total = jb["total"].ToString(),
                        rows = List
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var data = new
                    {
                        total = 0,
                        rows = List
                    };
                    return Json(data, JsonRequestBehavior.AllowGet);
                }

            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");

            }
        }
    }
}